<template>
	<div class="container">
		<h3>主体备案</h3>
		<el-row style="margin-top: 20px">
		    <el-col :span="7">
		        <el-input placeholder="请输入关键词" clearable v-model="searchName" style="width: 300px">
		            <i slot="prefix" class="el-input__icon el-icon-search"></i>
		            <el-button slot="append"  @click="handleSearch">搜索</el-button>
		        </el-input>
		    </el-col>
		    <el-button type="primary" @click="goadd" style="float: right; margin-right: 10px">新增</el-button>
		</el-row>

		<el-table :data="tableData" border ref="multipleTable" header-cell-class-name="table-header" style="margin-top: 20px">
		    <el-table-column prop="abbreviation" sortable label="企业名称"></el-table-column>
		    <el-table-column prop="legalName" sortable label="联系人姓名" width="120"></el-table-column>
		    <el-table-column prop="legalPhone" sortable label="联系人电话"  width="180"></el-table-column>
		    <el-table-column prop="area" sortable label="所在区域"></el-table-column>
		    <el-table-column prop="meetPeople" sortable label="授权状态">
				<template slot-scope="scope">
					<div v-if="scope.row.expiretimeEnd">已授权</div>
					<div v-else>未授权</div>
				</template>
			</el-table-column>
		    <el-table-column label="操作" width="330" align="center">
		        <template slot-scope="scope">
		            <el-button  type="text" style="color: #409EFF" @click="handleEdit(scope.row)">信息编辑</el-button>
					<el-button  type="text" style="color: #409EFF" @click="toFruit(scope.row)">产品编辑</el-button>
					<el-button  type="text" style="color: #409EFF" @click="handleWarranty(scope.$index, scope.row)">时间授权</el-button>
					<el-button  type="text" style="color: #409EFF" @click="handleInform(scope.$index, scope.row)">功能配置</el-button>
					<el-button  type="text" style="color: #409EFF" @click="handleReset(scope.$index, scope.row)">重置密码</el-button>
		        </template>
		    </el-table-column>
		</el-table>
		<el-pagination
		    layout="total, prev, pager, next, jumper"
		    :page-size="pagesize"
		    :current-page.sync="currentPage"
		    @current-change="handleCurrentChange"
		    :total="total"
		    style="margin-top: 20px"
		></el-pagination>

		<el-dialog
		  title="重置密码成功"
		  :visible.sync="dialogVisible"
		  width="30%"
		  :before-close="handleClose">
		  <div>账号：{{phone}}</div>
		  <div>密码：{{pwd}}</div>
		  <span slot="footer" class="dialog-footer">
		    <el-button @click="dialogVisible = false">我知道了</el-button>
		    <el-button type="primary" @click="copy">复制账号信息</el-button>
		  </span>
		</el-dialog>
	</div>
</template>

<script>
	import { CodeToText } from 'element-china-area-data';
	import {
		goverment_findByList,
		goverment_delete,
		company_resetpassword
	} from "@/request/supervisionCenterApi.js"
	export default{
		data(){
			return{
				dialogVisible:false,
				searchName:'',
				tableData:[],
				currentPage: 1,
				pagesize: 10,
				total: 0,
				phone:'',
				pwd:'',
				company:''
			}
		},
		mounted(){
			this.getData();
		},
		methods:{
			handleClose(){

			},
			copy(){
				var url =  this.company+" ;账号：" + this.phone +  ";密码：" + this.pwd + ";网址：http://app.cpzsyun.com"
				var domUrl = document.createElement("input");
				domUrl.value = url;
				domUrl.id = "creatDom";
				document.body.appendChild(domUrl);
				domUrl.select(); // 选择对象
				document.execCommand("Copy"); // 执行浏览器复制命令
				let creatDom = document.getElementById("creatDom");
				creatDom.parentNode.removeChild(creatDom);
				this.dialogVisible = false
				this.$message({
					type: 'success',
					message: '复制成功'
				});
			},
			// 获取列表数据
			getData(){
				let data = {
					examined:1,
				    type:"1",
				    page:this.currentPage
				}
				goverment_findByList(data).then((res)=>{
				    this.tableData = res.rows
				    this.total = res.total ;
					for (let i = 0; i < this.tableData.length; i++) {
					  var list = this.tableData[i].area.split(",")
					  var arr = ""
					  for (let k = 0; k < list.length; k++) {
					      arr += CodeToText[list[k]] + "/"
					  }
					    this.tableData[i].area = arr.substr(0,arr.length-1)
					}
				})
			},
			// 点击切换 页码
			handleCurrentChange(val) {
			    this.currentPage = val;
			    this.getData();
			},
			//  激活重置密码
			handleReset(index,row){

				this.$confirm('此操作不可逆, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					var pasArr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'];
					var password = '';
					var pasArrLen = pasArr.length;
					for (var i=0; i<6; i++){
						var x = Math.floor(Math.random()*pasArrLen);
						password += pasArr[x];
					}
					let data = {
						companyId:row.id,
						password:password
					}
					company_resetpassword(data).then((res)=>{
						this.phone = row.legalPhone
						this.pwd = password
						this.company = row.abbreviation
						this.dialogVisible = true
					})

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消重置'
					});
				});


			},

			/* 跳转到 功能配置 */
			handleInform(index,row){
				this.$router.push({
					path:'/supervisionCenterfiling/filing/configuration',
					query:{id:row.id}
				})
			},
			// 跳转到时间授权
			handleWarranty(index,row){
				this.$router.push({
					path:'/supervisionCenterfiling/filing/authorization',
					query:{id:row.id}
				})
			},
			//跳转到产品编辑
			toFruit(row){
				this.$router.push({
					path:'/supervisionCenterfiling/filing/compenyEdits',
					query:{id:row.id}
				})
			},
			// 跳转到信息编辑
			handleEdit(row){
				this.$router.push({
					path:'/supervisionCenterfiling/filing/edit',
					query:{id:row.id,name:row.abbreviation}
				})
			},
			// // 跳转到编辑
			// handleEdit(index,row){
			// 	this.$router.push({
			// 		path:'/supervisionCenterfiling/filing/edit',
			// 		query:{id:row.id}
			// 	})
			// },
			// 跳转到新增
			goadd(){
				this.$router.push({
					path:'/supervisionCenterfiling/filing/added'
				})
			},

			// 搜索
			handleSearch(){
				let searchData = {
					name: this.searchName,
                    examined:1,
				}
				goverment_findByList(searchData).then((res)=>{
				    this.tableData = res.rows
				    this.total = res.total ;
					for (let i = 0; i < this.tableData.length; i++) {
					  var list = this.tableData[i].area.split(",")
					  var arr = ""
					  for (let k = 0; k < list.length; k++) {
					      arr += CodeToText[list[k]] + "/"
					  }
					    this.tableData[i].area = arr.substr(0,arr.length-1)
					}
				})
			}
		}
	}
</script>

<style scoped>
	.el-pagination {
	    text-align: center;
	}
</style>
